package classes.controlers {
	import classes.managers.Game_Manager;
	import classes.sounds.SoundCollection;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.ui.Mouse;
	import flash.utils.Timer;
	/**
	 * ...
	 * @author Kihoonee
	 */
	public class Pointer_Controler extends A_Controler {
		
		private var coins:MovieClip;
		
		private var isPlay:Boolean = false;
		private var _isDown:Boolean = false;
		private var _coinTouch1:Boolean = false;
		private var _coinTouch2:Boolean = false;
		private var _coinTouch3:Boolean = false;
		
		private var onlyOne:Boolean = true;
		
		private var oldX:Number;
		private var oldY:Number;
		
		private var tenCount:Timer = new Timer(1000, 10 );
		
		public function Pointer_Controler( _gm:Game_Manager, _mc:MovieClip ) {
			super( _gm, _mc );
			
			mc.mouseEnabled = false;
			mc.mouseChildren = false;
			
			mc.alarm_mc.visible = false;
		}
		
		public function init( _coins:MovieClip ):void {
			coins = _coins;
			
			isPlay = false;
			isDown = false;
			coinTouch1 = false;
			coinTouch2 = false;
			coinTouch3 = false;
			onlyOne = true;
			mc.alarm_mc.visible = false;
			
			mc.gotoAndStop(1);
			
			tenCount.reset();
			tenCount.addEventListener(TimerEvent.TIMER, tenSecTIMERS );
			
			mc.addEventListener(Event.ENTER_FRAME, ENTER_FRAME );
		}
		
		public function play():void {
			if ( isPlay === false ) {
				if ( isDown ) {
					mc.gotoAndPlay(2);
					isPlay = true;
					
					oldX = mc.x;
					oldY = mc.y;
					
					tenCount.stop();
					tenCount.reset();
					tenCount.start();
					
				}
			}
		}
		
		public function stop():void {
			mc.gotoAndStop(1);
			isPlay = false;
		}
		
		private function ENTER_FRAME(e:Event):void {
			mc.x = mc.parent.mouseX;
			mc.y = mc.parent.mouseY;
			
			//10초간 움직임 체크.
			if ( oldX == mc.x && oldY == mc.y ) {
			}else {
				oldX = mc.x;
				oldY = mc.y;
					
				mc.alarm_mc.visible = false;
				
				gm.countUpdate( 10 );
				tenCount.stop();
				tenCount.reset();
				tenCount.start();
				if ( isPlay ) {
					if ( onlyOne ) {
						mc.gotoAndPlay(2);
						onlyOne = false;
					}
				}
			}
			
			if ( isDown ) {
				// 동전 먹기.
				if ( mc.hitArea_mc.hitTestObject( coins.coin10_mc1.hit_mc ) || mc.hitArea_mc.hitTestObject( coins.coin20_mc1.hit_mc ) || mc.hitArea_mc.hitTestObject( coins.coin30_mc1.hit_mc ) || 
					mc.hitArea_mc.hitTestObject( coins.coin40_mc1.hit_mc ) || mc.hitArea_mc.hitTestObject( coins.coin50_mc1.hit_mc ) || mc.hitArea_mc.hitTestObject( coins.coin60_mc1.hit_mc ) ||
					mc.hitArea_mc.hitTestObject( coins.coin100_mc1.hit_mc ) || mc.hitArea_mc.hitTestObject( coins.coin1000_mc1.hit_mc )) {
					if ( coinTouch1 === false ) {
						gm.getCoin("coin1");
						coinTouch1 = true;
					}
				}
				
				if ( mc.hitArea_mc.hitTestObject( coins.coin10_mc2.hit_mc ) || mc.hitArea_mc.hitTestObject( coins.coin20_mc2.hit_mc ) || mc.hitArea_mc.hitTestObject( coins.coin30_mc2.hit_mc ) || 
					mc.hitArea_mc.hitTestObject( coins.coin40_mc2.hit_mc ) || mc.hitArea_mc.hitTestObject( coins.coin50_mc2.hit_mc ) || mc.hitArea_mc.hitTestObject( coins.coin60_mc2.hit_mc ) ||
					mc.hitArea_mc.hitTestObject( coins.coin100_mc2.hit_mc ) || mc.hitArea_mc.hitTestObject( coins.coin1000_mc2.hit_mc )) {
					if ( coinTouch2 === false ) {
						gm.getCoin("coin2");
						coinTouch2 = true;
					}
				}
				
				if ( mc.hitArea_mc.hitTestObject( coins.coin10_mc3.hit_mc ) || mc.hitArea_mc.hitTestObject( coins.coin20_mc3.hit_mc ) || mc.hitArea_mc.hitTestObject( coins.coin30_mc3.hit_mc ) || 
					mc.hitArea_mc.hitTestObject( coins.coin40_mc3.hit_mc ) || mc.hitArea_mc.hitTestObject( coins.coin50_mc3.hit_mc ) || mc.hitArea_mc.hitTestObject( coins.coin60_mc3.hit_mc ) ||
					mc.hitArea_mc.hitTestObject( coins.coin100_mc3.hit_mc ) || mc.hitArea_mc.hitTestObject( coins.coin1000_mc3.hit_mc )) {
					if ( coinTouch3 === false ) {
						gm.getCoin("coin3");
						coinTouch3 = true;
					}
				}
			}
			
			if ( mc.parent.mouseX < 0 ) {
				mc.x = 0;
				Mouse.show();
			}else if ( mc.parent.mouseX > mc.parent["mask_mc"].width ) {
				mc.x = mc.parent["mask_mc"].width;
				Mouse.show();
			}else if ( mc.parent.mouseX > 0 && mc.parent.mouseX < mc.parent["mask_mc"].width ) {
				if ( mc.parent.mouseY > 0 && mc.parent.mouseY < mc.parent["mask_mc"].height ) {
					Mouse.hide();
				}
			}
			
			if ( mc.parent.mouseY < 0 ) {
				mc.y = 0;
				Mouse.show();
			}else if ( mc.parent.mouseY > mc.parent["mask_mc"].height ) {
				mc.y = mc.parent["mask_mc"].height;
				Mouse.show();
			}else if ( mc.parent.mouseY > 0 && mc.parent.mouseY < mc.parent["mask_mc"].height ) {
				if ( mc.parent.mouseX > 0 && mc.parent.mouseX < mc.parent["mask_mc"].width ) {
					Mouse.hide();
				}
			}
		}
		
		// 10초 시간 보여주기.
		private function tenSecTIMERS(e:TimerEvent):void {
			gm.countUpdate( 10 - tenCount.currentCount );
			
			if ( (10 - tenCount.currentCount) <= 4 ) {
				SoundCollection.clockSound();
				mc.alarm_mc.visible = true;
				mc.stop();
				onlyOne = true;
			}else {
				mc.alarm_mc.visible = false;
			}
			
			
			if ( tenCount.currentCount === 10 ) { // 10초간 안움지기믄 겜 오버
				if ( oldX == mc.x && oldY == mc.y ) {
					trace("pointer")
					//gm.rabbitWakeUp();
					gm.gameOver();
					//mc.removeEventListener(Event.ENTER_FRAME, ENTER_FRAME );
					//tenCount.removeEventListener(TimerEvent.TIMER_COMPLETE, tenSecTIMERS );
				}
			}
		}
		
		public function gameOver():void {
			mc.removeEventListener(Event.ENTER_FRAME, ENTER_FRAME );
			tenCount.reset();
			tenCount.stop();
			tenCount.removeEventListener(TimerEvent.TIMER_COMPLETE, tenSecTIMERS );
		}
		
		public function get isDown():Boolean { return _isDown; }
		public function set isDown(value:Boolean):void {
			_isDown = value;
		}
		
		public function get coinTouch1():Boolean { return _coinTouch1; }
		public function set coinTouch1(value:Boolean):void {
			_coinTouch1 = value;
		}
		
		public function get coinTouch2():Boolean { return _coinTouch2; }
		public function set coinTouch2(value:Boolean):void {
			_coinTouch2 = value;
		}
		
		public function get coinTouch3():Boolean { return _coinTouch3; }
		public function set coinTouch3(value:Boolean):void {
			_coinTouch3 = value;
		}
	}
}